//
//  BezierCurveView.m
//  iOSStudy
//
//  Created by Evan Xie on 12/2/15.
//  Copyright © 2015 Family Wings. All rights reserved.
//

#import "BezierCurveView.h"

@implementation BezierCurveView

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetAllowsAntialiasing(context, true);
    CGContextSetShouldAntialias(context, true);
    CGContextSetLineWidth(context, 2.0);
    
    CGFloat width = rect.size.width;
    
    CGRect cubicBezierBounds = CGRectMake(10, 10, width-20, 180);
    CGRect quadraticBezierBounds = CGRectMake(10, CGRectGetMaxY(cubicBezierBounds)+20, width-20, 180);
    
    CGContextSaveGState(context);
    
    // Draw two rectangle bounds.
    float dash[2] = {3, 3};
    CGContextSetLineDash(context, 10, dash, 2);
    CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
    CGContextStrokeRect(context, cubicBezierBounds);
    CGContextStrokeRect(context, quadraticBezierBounds);
    
    CGContextRestoreGState(context);
    
    // Draw cubic Bézier curve uses two control points [Part 1]
    CGContextMoveToPoint(context, cubicBezierBounds.origin.x+10, cubicBezierBounds.origin.y + CGRectGetHeight(cubicBezierBounds)/2);
    CGPoint controlPoint1 = {cubicBezierBounds.origin.x + CGRectGetWidth(cubicBezierBounds)/8, 10};
    CGPoint controlPoint2 = {cubicBezierBounds.origin.x + 3*CGRectGetWidth(cubicBezierBounds)/8, CGRectGetMaxY(cubicBezierBounds)-10};
    CGPoint endPoint = {cubicBezierBounds.origin.x + CGRectGetWidth(cubicBezierBounds)/2, cubicBezierBounds.origin.y + CGRectGetHeight(cubicBezierBounds)/2};
    CGContextAddCurveToPoint(context, controlPoint1.x, controlPoint1.y, controlPoint2.x, controlPoint2.y, endPoint.x, endPoint.y);
    CGContextSetStrokeColorWithColor(context, [UIColor magentaColor].CGColor);
    CGContextStrokePath(context);
    
    // [Part 2]
    CGPoint startPoint = endPoint;
    CGContextMoveToPoint(context, startPoint.x, startPoint.y);
    controlPoint1 = (CGPoint){cubicBezierBounds.origin.x + 5*CGRectGetWidth(cubicBezierBounds)/8, 10};
    controlPoint2 = (CGPoint){cubicBezierBounds.origin.x + 7*CGRectGetWidth(cubicBezierBounds)/10, cubicBezierBounds.origin.y+CGRectGetWidth(cubicBezierBounds)/2};
    endPoint = (CGPoint){CGRectGetMaxX(cubicBezierBounds)-10.0, cubicBezierBounds.origin.y + CGRectGetHeight(cubicBezierBounds)/2};
    CGContextAddCurveToPoint(context, controlPoint1.x, controlPoint1.y, controlPoint2.x, controlPoint2.y, endPoint.x, endPoint.y);
    CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
    CGContextStrokePath(context);
    
    // Draw quadratic Bézier curve uses one control point [Part 1]
    CGPoint controlPoint = {quadraticBezierBounds.origin.x + CGRectGetWidth(quadraticBezierBounds)/4, quadraticBezierBounds.origin.y};
    endPoint = (CGPoint){quadraticBezierBounds.origin.x + CGRectGetWidth(quadraticBezierBounds)/2,  quadraticBezierBounds.origin.y + CGRectGetHeight(quadraticBezierBounds)/2};
    CGContextMoveToPoint(context, quadraticBezierBounds.origin.x+10, quadraticBezierBounds.origin.y + CGRectGetHeight(quadraticBezierBounds)/2);
    CGContextAddQuadCurveToPoint(context, controlPoint.x, controlPoint.y, endPoint.x, endPoint.y);
    CGContextSetStrokeColorWithColor(context, [UIColor greenColor].CGColor);
    CGContextStrokePath(context);
    
    // [Part 2]
    startPoint = endPoint;
    controlPoint = (CGPoint){quadraticBezierBounds.origin.x + 3*CGRectGetWidth(quadraticBezierBounds)/4, CGRectGetMaxY(quadraticBezierBounds)};
    endPoint = (CGPoint){CGRectGetMaxX(quadraticBezierBounds) - 10.0,  quadraticBezierBounds.origin.y + CGRectGetHeight(quadraticBezierBounds)/2};
    CGContextMoveToPoint(context, startPoint.x, startPoint.y);
    CGContextAddQuadCurveToPoint(context, controlPoint.x, controlPoint.y, endPoint.x, endPoint.y);
    CGContextSetStrokeColorWithColor(context, [UIColor cyanColor].CGColor);
    CGContextStrokePath(context);
}

@end
